<?php

/**
 * 
 */
class Registration extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->helper('html');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('googlemaps');
        $this->load->helper('url');
        $this->load->model('register_model');
    }

    public function index() {
        $this->load->view('registration/register');
    }

    public function register() {
        if ($this->_submit_validate() === FALSE) {
            $data = array(
                'login' => form_error('login'),
                'pass' => form_error('pass'),
                'pass2' => form_error('pass2'),
                'email' => form_error('email')
            );
            echo json_encode($data);
        } else if ($this->register_model->register() > 0) {
            echo json_encode("Error");
        } else {
            echo json_encode("Ok");
        }
    }

    private function _submit_validate() {

        // validation rules
        $this->form_validation->set_rules('login', 'Username', 'required|alpha_numeric|min_length[5]|max_length[12]');

        $this->form_validation->set_rules('pass', 'Password', 'required|min_length[6]|max_length[12]');

        $this->form_validation->set_rules('pass2', 'Confirm Password', 'required|matches[pass]');

        $this->form_validation->set_rules('email', 'E-mail', 'required|valid_email');

        return $this->form_validation->run();
    }

}

?>